﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YJKCodeBase.WinForm;

namespace Balcony
{
	public partial class PolylineForm : System.Windows.Forms.Form
	{
		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		public PolylineForm.OperateType CurrentOperateType
		{
			get
			{
				return this.m_CurrentOperateType;
			}
		}

		public PolylineForm(WindowHandle wndRevit)
		{
			this._hWndRevit = wndRevit;
			this.InitializeComponent();
		}

		private void EditBaseLineForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void EditBaseLineForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void EditBaseLineForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		public void SetDrawLineType()
		{
			this.tsbDrawLine.CheckState = CheckState.Checked;
			this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			this.tsbDelete.CheckState = CheckState.Unchecked;
			this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;
			this.m_PickButton = true;
		}

		private void tsbDrawLine_Click(object sender, EventArgs e)
		{
			this.tsbDrawLine.CheckState = CheckState.Checked;
			this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			this.tsbDelete.CheckState = CheckState.Unchecked;
			this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void tsbDrawArc3Pt_Click(object sender, EventArgs e)
		{
			this.tsbDrawLine.CheckState = CheckState.Unchecked;
			this.tsbDrawArc3Pt.CheckState = CheckState.Checked;
			this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			this.tsbDelete.CheckState = CheckState.Unchecked;
			this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawArc3Pt;
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawArcCenter_Click(object sender, EventArgs e)
		{
			this.tsbDrawLine.CheckState = CheckState.Unchecked;
			this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			this.tsbDrawArcCenter.CheckState = CheckState.Checked;
			this.tsbDelete.CheckState = CheckState.Unchecked;
			this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawArcCenter;
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDelete_Click(object sender, EventArgs e)
		{
			this.tsbDrawLine.CheckState = CheckState.Unchecked;
			this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			this.tsbDelete.CheckState = CheckState.Checked;
			this.m_CurrentOperateType = PolylineForm.OperateType.OT_Delete;
			this.m_PickButton = true;
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "PolylineForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.tsbDrawLine.CheckState = CheckState.Checked;
				this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new Point(x, y);
			PolylineForm.m_Version = Convert.ToInt32(streamReader.ReadLine());
			if (PolylineForm.m_Version == 1)
			{
				this.m_CurrentOperateType = (PolylineForm.OperateType)Convert.ToInt32(streamReader.ReadLine());
				if (this.m_CurrentOperateType == PolylineForm.OperateType.OT_Delete)
				{
					this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;
				}
				this.tsbDrawLine.CheckState = CheckState.Unchecked;
				this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
				this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
				this.tsbDelete.CheckState = CheckState.Unchecked;
				switch (this.m_CurrentOperateType)
				{
				case PolylineForm.OperateType.OT_DrawLine:
					this.tsbDrawLine.CheckState = CheckState.Checked;
					break;
				case PolylineForm.OperateType.OT_DrawArc3Pt:
					this.tsbDrawArc3Pt.CheckState = CheckState.Checked;
					break;
				case PolylineForm.OperateType.OT_DrawArcCenter:
					this.tsbDrawArcCenter.CheckState = CheckState.Checked;
					break;
				case PolylineForm.OperateType.OT_Delete:
					this.tsbDelete.CheckState = CheckState.Checked;
					break;
				}
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "PolylineForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(PolylineForm.m_Version);
			if (this.m_CurrentOperateType == PolylineForm.OperateType.OT_Delete)
			{
				this.m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;
			}
			streamWriter.WriteLine((int)this.m_CurrentOperateType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void EditBaseLineForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void SimulationPressESC()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			PolylineForm.keybd_event(bVk, 0, dwFlags, 0);
			PolylineForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			PolylineForm.SendMessage(this._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void tsbDrawLine_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArc3Pt_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArcCenter_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDelete_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private static int m_Version = 1;

		private WindowHandle _hWndRevit;

		private PolylineForm.OperateType m_CurrentOperateType = PolylineForm.OperateType.OT_DrawLine;

		private bool m_PickButton;

		public enum OperateType
		{
			OT_DrawLine = 1,
			OT_DrawArc3Pt,
			OT_DrawArcCenter,
			OT_Delete
		}
	}
}
